package com.itheima.project.service.impl;

import com.itheima.project.pojo.ContentChannel;
import com.itheima.project.mapper.ContentChannelMapper;
import com.itheima.project.service.IContentChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.itheima.project.vo.content.ContentChannelVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @Description：交易渠道表服务实现类
 */
@Service
public class ContentChannelServiceImpl extends ServiceImpl<ContentChannelMapper, ContentChannel> implements IContentChannelService {

    @Override
    public Page<ContentChannel> findContentChannelPage(ContentChannelVo contentChannelVo, int pageNum, int pageSize) {
        //构建分页对象
        Page<ContentChannel> page = new Page<>(pageNum,pageSize);
        //构建查询条件
        QueryWrapper<ContentChannel> queryWrapper = new QueryWrapper<>();
        //构建多条件查询，代码生成后自己可自行调整
        //安全审核通道名称查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getChannelName())) {
            queryWrapper.lambda().eq(ContentChannel::getChannelName,contentChannelVo.getChannelName());
        }
        //通道唯一标记查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getChannelLabel())) {
            queryWrapper.lambda().eq(ContentChannel::getChannelLabel,contentChannelVo.getChannelLabel());
        }
        //域名查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getDomain())) {
            queryWrapper.lambda().eq(ContentChannel::getDomain,contentChannelVo.getDomain());
        }
        //商户appid查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getAppId())) {
            queryWrapper.lambda().eq(ContentChannel::getAppId,contentChannelVo.getAppId());
        }
        //内容审核公钥查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getPublicKey())) {
            queryWrapper.lambda().eq(ContentChannel::getPublicKey,contentChannelVo.getPublicKey());
        }
        //内容审核私钥查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getMerchantPrivateKey())) {
            queryWrapper.lambda().eq(ContentChannel::getMerchantPrivateKey,contentChannelVo.getMerchantPrivateKey());
        }
        //其他配置查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getOtherConfig())) {
            queryWrapper.lambda().eq(ContentChannel::getOtherConfig,contentChannelVo.getOtherConfig());
        }
        //说明查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getRemark())) {
            queryWrapper.lambda().eq(ContentChannel::getRemark,contentChannelVo.getRemark());
        }
        //状态查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getEnableFlag())) {
            queryWrapper.lambda().eq(ContentChannel::getEnableFlag,contentChannelVo.getEnableFlag());
        }
        //按创建时间降序
        queryWrapper.lambda().orderByDesc(ContentChannel::getCreatedTime);
        //执行分页查询
        return page(page, queryWrapper);
    }

    @Override
    public ContentChannel createContentChannel(ContentChannelVo contentChannelVo) {
        //转换ContentChannelVo为ContentChannel
        ContentChannel contentChannel = BeanConv.toBean(contentChannelVo, ContentChannel.class);
        boolean flag = save(contentChannel);
        if (flag){
        return contentChannel;
        }
        return null;
    }

    @Override
    public Boolean updateContentChannel(ContentChannelVo contentChannelVo) {
        //转换ContentChannelVo为ContentChannel
        ContentChannel contentChannel = BeanConv.toBean(contentChannelVo, ContentChannel.class);
        return updateById(contentChannel);
    }

    @Override
    public Boolean deleteContentChannel(String[] checkedIds) {
        //转换数组为集合
        List<String> ids = Arrays.asList(checkedIds);
        List<Long> idsLong = new ArrayList<>();
        ids.forEach(n->{
            idsLong.add(Long.valueOf(n));
        });
        return removeByIds(idsLong);
    }

    @Override
    public List<ContentChannel> findContentChannelList(ContentChannelVo contentChannelVo) {
        //构建查询条件
        QueryWrapper<ContentChannel> queryWrapper = new QueryWrapper<>();
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getId())) {
            queryWrapper.lambda().eq(ContentChannel::getId,contentChannelVo.getId());
        }
        //安全审核通道名称查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getChannelName())) {
            queryWrapper.lambda().eq(ContentChannel::getChannelName,contentChannelVo.getChannelName());
        }
        //通道唯一标记查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getChannelLabel())) {
            queryWrapper.lambda().eq(ContentChannel::getChannelLabel,contentChannelVo.getChannelLabel());
        }
        //域名查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getDomain())) {
            queryWrapper.lambda().eq(ContentChannel::getDomain,contentChannelVo.getDomain());
        }
        //商户appid查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getAppId())) {
            queryWrapper.lambda().eq(ContentChannel::getAppId,contentChannelVo.getAppId());
        }
        //内容审核公钥查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getPublicKey())) {
            queryWrapper.lambda().eq(ContentChannel::getPublicKey,contentChannelVo.getPublicKey());
        }
        //内容审核私钥查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getMerchantPrivateKey())) {
            queryWrapper.lambda().eq(ContentChannel::getMerchantPrivateKey,contentChannelVo.getMerchantPrivateKey());
        }
        //其他配置查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getOtherConfig())) {
            queryWrapper.lambda().eq(ContentChannel::getOtherConfig,contentChannelVo.getOtherConfig());
        }
        //说明查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getRemark())) {
            queryWrapper.lambda().eq(ContentChannel::getRemark,contentChannelVo.getRemark());
        }
        //状态查询
        if (!EmptyUtil.isNullOrEmpty(contentChannelVo.getEnableFlag())) {
            queryWrapper.lambda().eq(ContentChannel::getEnableFlag,contentChannelVo.getEnableFlag());
        }
        return list(queryWrapper);
    }
}
